// $Id: query.cpp 9 2008-09-30 03:21:47Z rodlima $

/*
   Copyright (c) 2008, Rodolfo Schulz de Lima 
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.  
   * Neither the name of RodSoft nor the names of its contributors may be used
     to endorse or promote products derived from this software without specific
     prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.
*/

#include "pch.h"
#include "metainfo.h"
#include "query.h"

namespace orm
{

namespace expr
{
set_factor::set_factor(const set_factor &that)/*{{{*/
{
    m_factors.reserve(that.m_factors.size());
    for(unsigned i=0; i<that.m_factors.size(); ++i)
        m_factors.push_back(that.m_factors[i]->clone());
}/*}}}*/

std::string member_base::get_value(const transaction &xa) const/*{{{*/
{
    return m_member->query_name();
}/*}}}*/

std::string unary_term::do_get_term_value(const transaction &xa) const/*{{{*/
{
    std::ostringstream ss;
    if(m_parentheses)
        ss << "(";

    ss << m_op << " " << m_term->value(xa);
    if(m_parentheses)
        ss << ")";
    return ss.str();
}/*}}}*/
std::string binary_term::do_get_term_value(const transaction &xa) const/*{{{*/
{
    std::ostringstream ss;
    if(m_parentheses)
        ss << "(";
    ss << m_term1->value(xa) << " " << m_op << " " << m_term2->value(xa);
    if(m_parentheses)
        ss << ")";
    return ss.str();
}/*}}}*/
std::string unary_factor::do_get_factor_value(const transaction &xa) const/*{{{*/
{
    std::ostringstream ss;
    if(m_parentheses)
        ss << "(";
    ss << m_op << " " << m_factor->value(xa);
    if(m_parentheses)
        ss << ")";
    return ss.str();
}/*}}}*/
std::string binary_factor::do_get_factor_value(const transaction &xa) const/*{{{*/
{
    std::ostringstream ss;
    if(m_parentheses)
        ss << "(";
    ss << m_factor1->value(xa) << " " << m_op << " " << m_factor2->value(xa);
    if(m_parentheses)
        ss << ")";
    return ss.str();
}/*}}}*/
std::string factor_binary_term::do_get_term_value(const transaction &xa) const/*{{{*/
{
    std::ostringstream ss;
    if(m_parentheses)
        ss << "(";
    ss << m_factor1->value(xa) << " " << m_op << " " << m_factor2->value(xa);
    if(m_parentheses)
        ss << ")";
    return ss.str();
}/*}}}*/
std::string set_factor::do_get_factor_value(const transaction &xa) const/*{{{*/
{
    std::ostringstream ss;
    ss << "(";
    for(unsigned i=0; i<m_factors.size(); ++i)
    {
        ss << m_factors[i]->value(xa);
        if(i < m_factors.size()-1)
            ss << ",";
    }
    ss << ")";
    return ss.str();
}/*}}}*/
std::string function_factor::do_get_factor_value(const transaction &xa) const/*{{{*/
{
    std::ostringstream ss;
    ss << m_name << m_args.value(xa);
    return ss.str();
}/*}}}*/

} // namespace expr

using namespace expr;

binary_term operator&&(boolean_comparable &&b1, boolean_comparable &&b2)/*{{{*/
{
    return binary_term(b1.get_term(), b2.get_term(), "AND");
}/*}}}*/
binary_term operator||(boolean_comparable &&b1, boolean_comparable &&b2)/*{{{*/
{
    return binary_term(b1.get_term(), b2.get_term(), "OR");
}/*}}}*/
unary_term operator!(boolean_comparable &&b)/*{{{*/
{
    return unary_term(b.get_term(), "NOT");
}/*}}}*/

factor_binary_term operator==(value &&c1, equality_comparable &&c2)/*{{{*/
{
    return factor_binary_term(c1.get_factor(), c2.get_factor(), c2.equal_op());
}/*}}}*/
factor_binary_term operator!=(value &&c1, equality_comparable &&c2)/*{{{*/
{
    return factor_binary_term(c1.get_factor(), c2.get_factor(), c2.not_equal_op());
}/*}}}*/
factor_binary_term operator<(value &&c1, less_than_comparable &&c2)/*{{{*/
{
    return factor_binary_term(c1.get_factor(), c2.get_factor(), c2.less_than_op());
}/*}}}*/
factor_binary_term operator>(value &&c1, less_than_comparable &&c2)/*{{{*/
{
    return factor_binary_term(c1.get_factor(), c2.get_factor(), c2.greater_than_op());
}/*}}}*/
factor_binary_term operator<=(value &&c1, less_than_comparable &&c2)/*{{{*/
{
    return factor_binary_term(c1.get_factor(), c2.get_factor(), c2.less_or_equal_than_op());
}/*}}}*/
factor_binary_term operator>=(value &&c1, less_than_comparable &&c2)/*{{{*/
{
    return factor_binary_term(c1.get_factor(), c2.get_factor(), c2.greater_or_equal_than_op());
}/*}}}*/

binary_factor operator+(value &&f1, value &&f2)/*{{{*/
{
    return binary_factor(f1.get_factor(), f2.get_factor(), "+");
}/*}}}*/
binary_factor operator-(value &&f1, value &&f2)/*{{{*/
{
    return binary_factor(f1.get_factor(), f2.get_factor(), "-");
}/*}}}*/
unary_factor operator-(value &&f1)/*{{{*/
{
    return unary_factor(f1.get_factor(), "-");
}/*}}}*/
binary_factor operator*(value &&f1, value &&f2)/*{{{*/
{
    return binary_factor(f1.get_factor(), f2.get_factor(), "*");
}/*}}}*/
binary_factor operator/(value &&f1, value &&f2)/*{{{*/
{
    return binary_factor(f1.get_factor(), f2.get_factor(), "/");
}/*}}}*/
binary_factor operator%(value &&f1, value &&f2)/*{{{*/
{
    return binary_factor(f1.get_factor(), f2.get_factor(), "%");
}/*}}}*/
binary_factor operator^(value &&f1, value &&f2)/*{{{*/
{
    return binary_factor(f1.get_factor(), f2.get_factor(), "#");
}/*}}}*/
binary_factor operator&(value &&f1, value &&f2)/*{{{*/
{
    return binary_factor(f1.get_factor(), f2.get_factor(), "&");
}/*}}}*/
binary_factor operator|(value &&f1, value &&f2)/*{{{*/
{
    return binary_factor(f1.get_factor(), f2.get_factor(), "|");
}/*}}}*/
unary_factor operator~(value &&f1)/*{{{*/
{
    return unary_factor(f1.get_factor(), "~");
}/*}}}*/
binary_factor operator<<(value &&f1, value &&f2)/*{{{*/
{
    return binary_factor(f1.get_factor(), f2.get_factor(), "<<");
}/*}}}*/
binary_factor operator>>(value &&f1, value &&f2)/*{{{*/
{
    return binary_factor(f1.get_factor(), f2.get_factor(), ">>");
}/*}}}*/

equality_comparable like(const std::string &str)/*{{{*/
{
    return equality_comparable(str,"LIKE","NOT LIKE");
}/*}}}*/
equality_comparable ilike(const std::string &str)/*{{{*/
{
    return equality_comparable(str,"ILIKE","NOT ILIKE");
}/*}}}*/
equality_comparable similar(const std::string &str)/*{{{*/
{
    return equality_comparable(str,"SIMILAR","NOT SIMILAR");
}/*}}}*/
equality_comparable between(value &&val1, value &&val2)/*{{{*/
{
    return equality_comparable(
        binary_factor(val1.get_factor(), val2.get_factor(), "AND", false),
        "BETWEEN", "NOT BETWEEN");
}/*}}}*/

function_factor sin(value &&val)/*{{{*/
{
    return function_factor("sin",val.get_factor());
}/*}}}*/
function_factor cos(value &&val)/*{{{*/
{
    return function_factor("cos",val.get_factor());
}/*}}}*/
function_factor tan(value &&val)/*{{{*/
{
    return function_factor("tan",val.get_factor());
}/*}}}*/
function_factor asin(value &&val)/*{{{*/
{
    return function_factor("asin",val.get_factor());
}/*}}}*/
function_factor acos(value &&val)/*{{{*/
{
    return function_factor("acos",val.get_factor());
}/*}}}*/
function_factor atan(value &&val)/*{{{*/
{
    return function_factor("atan",val.get_factor());
}/*}}}*/
function_factor atan2(value &&val1, value &&val2)/*{{{*/
{
    return function_factor("atan",val1.get_factor(), val2.get_factor());
}/*}}}*/
function_factor pow(value &&val1, value &&val2)/*{{{*/
{
    return function_factor("power",val1.get_factor(), val2.get_factor());
}/*}}}*/
function_factor abs(value &&val)/*{{{*/
{
    return function_factor("abs",val.get_factor());
}/*}}}*/
function_factor ceil(value &&val)/*{{{*/
{
    return function_factor("ceil",val.get_factor());
}/*}}}*/
function_factor floor(value &&val)/*{{{*/
{
    return function_factor("floor",val.get_factor());
}/*}}}*/
function_factor degrees(value &&val)/*{{{*/
{
    return function_factor("degrees",val.get_factor());
}/*}}}*/
function_factor exp(value &&val)/*{{{*/
{
    return function_factor("exp",val.get_factor());
}/*}}}*/
function_factor log(value &&val)/*{{{*/
{
    return function_factor("ln",val.get_factor());
}/*}}}*/
function_factor log10(value &&val)/*{{{*/
{
    return function_factor("log",val.get_factor());
}/*}}}*/
function_factor radians(value &&val)/*{{{*/
{
    return function_factor("radians",val.get_factor());
}/*}}}*/
function_factor round(value &&val)/*{{{*/
{
    return function_factor("round",val.get_factor());
}/*}}}*/
function_factor sign(value &&val)/*{{{*/
{
    return function_factor("sign",val.get_factor());
}/*}}}*/
function_factor sqrt(value &&val)/*{{{*/
{
    return function_factor("sqrt",val.get_factor());
}/*}}}*/
function_factor trunc(value &&val)/*{{{*/
{
    return function_factor("trunc",val.get_factor());
}/*}}}*/

function_factor strlen(value &&val)/*{{{*/
{
    return function_factor("length",val.get_factor());
}/*}}}*/
binary_factor strcat(value &&val1,value &&val2)/*{{{*/
{
    return binary_factor(val1.get_factor(), val2.get_factor(), "||");
}/*}}}*/
function_factor strlwr(value &&val)/*{{{*/
{
    return function_factor("lower",val.get_factor());
}/*}}}*/
function_factor strupr(value &&val)/*{{{*/
{
    return function_factor("upper",val.get_factor());
}/*}}}*/
function_factor strtrim(value &&val)/*{{{*/
{
    return function_factor("trim",val.get_factor());
}/*}}}*/
function_factor md5(value &&val)/*{{{*/
{
    return function_factor("md5",val.get_factor());
}/*}}}*/
}  // namespace orm

